<?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Konten_statis extends CI_Controller
{
    
    function __construct() {
        parent::__construct();
        $this->load->model('m_konten_statis');
        $this->load->library('breadcrumb');
    }
    
    function index() {
        
        //rule type pada function ini: Read
        $this->rule->type('R');
        
        //title
        $this->layout->set_title('Konten Statis');
        
        //breadcrumb/untuk navigasi
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb('Dashboard', site_url('admin'));
        $this->breadcrumb->add_crumb('Konten', site_url('konten_statis#'));
        $this->breadcrumb->add_crumb('Konten Statis');
        
        //paging dan ambil data dari model
        $this->load->library('paging');
        $config['sql'] = $this->m_konten_statis->get();
        $config['per_page'] = 15;
        $data = $this->paging->create_pagination($config);
        
        //judul besar
        $data['primary_title'] = '<i class="fa fa-align-left"></i> Konten';
        $data['sub_primary_title'] = 'Pengaturan konten E-Jamkesda';
        
        //menghilangkan session pencarian jika ada
        $this->session->unset_userdata('keyword');
        
        //trim text, tanpa motong kata dan menghilangkan tag html
        $this->load->helper('trim_text');

        //menggunakan theme back
        $this->layout->theme('back', 'konten_statis/index', $data);
    }
    
    function edit($id_konten_statis = '') {
        
        //rule type pada function ini: Update
        $this->rule->type('U');
        
        if (isset($_POST) && !empty($_POST)) {
            
            $this->load->helper('slug');
            //ambil data dari form
            $id_konten_statis = $this->input->post('id_konten_statis');
            $dataUpdate = array(
            	'judul' => $this->input->post('judul'),
            	'konten' => $this->input->post('konten'),
                //pengecekan slug, slug($string, $table.field)
                'slug' => slug($this->input->post('judul'), 'konten_statis.slug', 'id_konten_statis', $id_konten_statis),
                'waktu' => date('Y-m-d H:i:s'),
            	'id_user' => $this->ion_auth->user()->row()->id
            	);
            
            if (!$this->m_konten_statis->update($id_konten_statis, $dataUpdate)) {
                $this->session->set_flashdata('gagal', '<b>' . $this->input->post('judul') . '</b> gagal diperbaharui');
                redirect($this->input->post('redirurl'), 'refresh');
            } else {
                $this->session->set_flashdata('sukses', '<b>' . $this->input->post('judul') . '</b> berhasil diperbaharui');
                redirect($this->input->post('redirurl'), 'refresh');
            }

        //untuk form edit
        } else {
            
            //parameter yang dipelukan query
            $params = array($id_konten_statis);
            
            //title
            $this->layout->set_title('Edit Konten Statis');
            
            //js tambahan, yntuk validasi
            $this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
            $this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
            
            //CKEditor
            $this->layout->set_include('js', 'themes/back/bundle/ckeditor/ckeditor.js', 'header');
            $this->layout->set_include('js', 'themes/back/bundle/ckeditor/lang/id.js', 'header');

            //breadcrumb/untuk navigasi
            $this->breadcrumb->clear();
            $this->breadcrumb->add_crumb('Dashboard', site_url('admin'));
            $this->breadcrumb->add_crumb('Konten', site_url('konten_statis/edit/'.$id_konten_statis.'#'));
            $this->breadcrumb->add_crumb('Konten Statis', site_url('konten_statis'));
            $this->breadcrumb->add_crumb('Edit Konten Statis');
            
            //judul besar
            $data['primary_title'] = '<i class="fa fa-align-left"></i> Konten';
            $data['sub_primary_title'] = 'Pengaturan konten E-Jamkesda';
            
            //ambil data dari model dengan berdasarkan parameter diatas
            $data['results'] = $this->m_konten_statis->get_by($params);
            //menggunakan theme back
            $this->layout->theme('back', 'konten_statis/edit', $data);
        }
    }
}
